<?php
/**
 * This file is part of php-http-proxy.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use \Workerman\Worker;
use \Workerman\Autoloader;
// require_once __DIR__ . '/../../vendor/autoload.php';
$worker = new Worker('tcp://127.0.0.1:8080');
$worker->count = 6;
$worker->name = 'AppHttpProxy';
$worker->onMessage = function ($connection, $message) 
{
    try {
        $data = json_decode($message, true);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $data['url']);
        curl_setopt($ch, CURLOPT_POST, $data['method'] == 'post' ? 1 : 0);
        // curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data['params']);
        curl_exec($ch);
        curl_close($ch);
    } catch (\Exception $e) {
        echo $e->getMessage();
    }
};

if (!defined('GLOBAL_START')) {
    Worker::runAll();
}
